package GUI.Show;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import GUI.Season.SeasonController;

import data.Show;

public class ShowController implements MouseListener {

	private Show show;
	private JFrame mainFrame;
	private ShowTableModel showTableModel;
	private JTable showTable;
	private JScrollPane showTableScrollPane;
	private Point startP;
	
	public ShowController(Show s, Point startP) 
	{
		this.startP = startP;
		this.show = s;
		this.mainFrame = new JFrame(s.getShowName());

		this.showTableModel = new ShowTableModel(this.getShowColumnNames(), s);
		
		this.showTable = new JTable(showTableModel);
  
		this.showTableModel.renderTable(this.showTable, s);
		this.showTable.addMouseListener(this);
		//this.selectionModel.addListSelectionListener(this);
		this.showTableScrollPane = new JScrollPane(showTable);
		
		
		//this.mainPanel.add(showTableScrollPane);
		this.mainFrame.add(showTableScrollPane);
		this.mainFrame.pack();
		this.mainFrame.setLocation(startP);
		new ShowView(mainFrame);
	}


	public String[] getShowColumnNames()
	{
		String[] returnString =
		{
				"Season Number"
		};
		return returnString;
	}

	@Override
	public void mouseClicked(MouseEvent e) 
	{
		if(e.getClickCount() >= 2)
		{
			Object obj = this.showTableModel.getValueAt(showTable.getSelectedRow(), showTable.getSelectedColumn());
			if(obj instanceof String)
			{
				String dataString = (String) obj;
				if(dataString.startsWith("Season "))
				{
					dataString = dataString.replace("Season ", "");
					int seasonNumber = Integer.parseInt(dataString);
					new SeasonController(show, show.getSeason(seasonNumber).getSeasonNumber(), show.getShowName(), startP);
					this.windowClose();
				}
			}		
		}
	}


	private void windowClose() 
	{
		this.mainFrame.dispose();
		this.mainFrame = null;
		this.show = null;
		this.showTable = null;
		this.showTableModel = null;
		this.showTableScrollPane = null;
	}


	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}
	@Override
	public void mouseReleased(MouseEvent arg0) {}

}
